package pile.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.web.multipart.MultipartFile;
import pile.customer.entity.User;
import pile.customer.mapper.UserMapper;
import pile.customer.params.frequentlyAskedQuestions.SentMessageDTO;
import pile.customer.service.IInformationService;
import pile.customer.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pile.customer.entity.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import pile.customer.entity.PageList;

import pile.customer.params.user.ListDTO;
import pile.customer.utils.DatesUtil;
import pile.customer.utils.OSSUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 郭天义
 * @since 2024-06-01
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OSSUtil ossUtil;


    @Override
    public ResponseResult listPage(ListDTO listDTO) {
        IPage<User> iPage = new Page(listDTO.getPage(), listDTO.getLimit());

        //根据最后一次发送信息倒序查询  客服信息互通
        IPage data = userMapper.pageList(iPage, listDTO);
        PageList pageList = new PageList();
        pageList.setCount(data.getTotal());
        pageList.setLimit(data.getSize());
        pageList.setList(data.getRecords());
        pageList.setPage(data.getCurrent());
        pageList.setPages(data.getPages());

        return new ResponseResult(pageList);
    }


    @Override
    public ResponseResult add(User user) {
        userMapper.insert(user);
        return new ResponseResult(200, "新增成功");
    }

    /**
     * 用户登录
     * @param openId
     * @param channel
     * @param phone
     */
    @Override
    public void userLogin(String openId,Integer channel,String phone) {

        if (openId == null) {
            return;
        }
        //先查询是否存在 不存在添加  存在不需要操作
        //根据 openId 查询
        User user = userMapper.selectByOpenId(openId);
        if (user != null) {
            //修改为在线
            user.setIsOnLine(1);

            userMapper.updateById(user);
            return;
        }
        //不存在添加  新用户肯定没有发送过消息
        User userAdd = new User();
        userAdd.setPhone(phone);
        //opneid
        userAdd.setOpenId(openId);
        //渠道
        userAdd.setChannel(channel);
        //在线
        userAdd.setIsOnLine(1);
        userMapper.insert(userAdd);



    }

    @Override
    public void userOffline(String openId) {

        //根据openid 修改用户在线状态为离线
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("open_id",openId);
        updateWrapper.set("is_on_line",2);
        //修改数据库
        update(updateWrapper);
    }

    /**
     * 查询未接入用户
     * @param listDTO
     * @return
     */
    @Override
    public ResponseResult userNotConnected(ListDTO listDTO) {

        List<User> userList = userMapper.selectUserNotConnected( listDTO);
        return new ResponseResult(userList);
    }

    /**
     * 根据openid查询用户信息
     * @param openId
     * @return
     */
    @Override
    public User selectByOpenId(String openId) {

        return userMapper.selectByOpenId(openId);
    }

    /**
     * 点击接入
     * @param listDTO
     * @return
     */
    @Override
    public ResponseResult clickToConnect(ListDTO listDTO) {
        User user = userMapper.selectByOpenId(listDTO.getOpenId());
        if(user == null){
           return new ResponseResult(203,"用户不存在");
        }
        //修改用户得待接入数量为 0
        user.setNoAccessNum(0);
        //  是否显示为 显示
        user.setIsDisplay(1);
        //未读消息为0
        user.setUnreadQuantity(0);
        //客服账户
        user.setCustomerServiceAccount(listDTO.getCustomerServiceAccount());
        userMapper.updateById(user);
        return new ResponseResult(200,"接入成功");
    }

    /**
     * 结束会话
     * @param listDTO
     * @return
     */
    @Override
    public ResponseResult endSession(ListDTO listDTO) {

        if(listDTO.getOpenId()==null){
            return new ResponseResult(203,"用户不存在");
        }
        //结束回话  修改用户里面的客服账户为null 是否显示改为不显示

        User user = userMapper.selectByOpenId(listDTO.getOpenId());
        if(user == null){
            return new ResponseResult(203,"用户不存在");
        }
        user.setCustomerServiceAccount(null);
        user.setIsDisplay(2);
        userMapper.updateById(user);
        return new ResponseResult(200,"结束会话成功");
    }

    /**
     * 上传图片
     * @param
     * @return
     */
    @Override
    public ResponseResult addFile(MultipartFile uploadFile) {
        String filePath = "";

        // 对上传的文件重命名，避免文件重名
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID().toString()
                + oldName.substring(oldName.lastIndexOf("."));

        try{
            //用阿里云oss
            String url = ossUtil.uploadImg2Oss(uploadFile, newName);

            return new ResponseResult(200, url);

        }catch (Exception e){
            log.error("上传文件报错:"+e);
            return new ResponseResult(203,"上传文件报错");
        }
    }

    /**
     * 是否已读
     * @param listDTO
     * @return
     */
    @Override
    public ResponseResult isRead(ListDTO listDTO) {
        User user = userMapper.selectByOpenId(listDTO.getOpenId());
        if(user == null){
            return new ResponseResult(203,"用户不存在");
        }
        user.setCustomerServiceAccount(listDTO.getCustomerServiceAccount());

        //是否已读
        user.setIsReply(2);
        user.setUnreadQuantity(0);
        userMapper.updateById(user);

        return new ResponseResult(200,"结束会话成功");
    }


}
